package com.study.studyelasticsearch.util.es.extractor;

import com.study.studyelasticsearch.util.es.extractor.vo.BucketItemVo;
import com.study.studyelasticsearch.util.es.extractor.vo.BucketVo;
import com.study.studyelasticsearch.util.es.extractor.vo.MaxBucketItemVo;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram;
import org.elasticsearch.search.aggregations.bucket.range.InternalDateRange;
import org.elasticsearch.search.aggregations.bucket.range.InternalRange;
import org.elasticsearch.search.aggregations.bucket.terms.DoubleTerms;
import org.elasticsearch.search.aggregations.bucket.terms.LongTerms;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.StringTerms;
import org.elasticsearch.search.aggregations.metrics.InternalMax;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 提取检索结果中aggr中的数据
 * 提取聚类结果
 *
 * @author 吴宇亮 on 2022/5/27 17:14
 */
@Component
public class EsAggrExtractor {

    public BucketVo extract(SearchResponse searchResponse) {
        BucketVo bucketVo = new BucketVo();
        Aggregations aggrs = searchResponse.getAggregations();
        if (aggrs.iterator().hasNext()) {
            this.extractRecursive(bucketVo, aggrs);
        }
        return bucketVo;
    }

    /**
     * 递归提取桶中的内容（桶中桶）
     */
    private void extractRecursive(BucketVo bucketVo, Aggregations aggrs) {
        aggrs.getAsMap().forEach((bucketName, aggr) -> {
            // group by name(String)
            if (aggr instanceof StringTerms) {
                List<StringTerms.Bucket> buckets = ((StringTerms) aggr).getBuckets();
                for (StringTerms.Bucket bucket : buckets) {
                    BucketItemVo bucketItemVo = new BucketItemVo(bucket.getKeyAsString(), bucket.getDocCount());
                    bucketVo.addBucketItem(bucketItemVo);
                    // 如果该bucket下还有子bucket，递归解析
                    Aggregations subAggrs = bucket.getAggregations();
                    if (subAggrs.iterator().hasNext()) {
                        BucketVo subBucketVo = new BucketVo();
                        bucketItemVo.setBucket(subBucketVo);
                        this.extractRecursive(subBucketVo, subAggrs);
                    }
                }
            }
            // group by age(long)
            else if(aggr instanceof LongTerms){
            }
            else if(aggr instanceof DoubleTerms) {
            }
            else if(aggr instanceof ParsedStringTerms) {

            }
            // 统计最大值
            else if (aggr instanceof InternalMax) {
                MaxBucketItemVo maxBucketItem = new MaxBucketItemVo();
                maxBucketItem.setKey(aggr.getName());
                maxBucketItem.setValue(((InternalMax) aggr).value());
                bucketVo.addBucketItem(maxBucketItem);
            }
            else if(aggr instanceof InternalDateRange){
            }
            else if(aggr instanceof InternalRange){
            }
            else if(aggr instanceof InternalDateHistogram){
            }
        });
    }


}
